package com.doit.demo02;

public class test_volatile2 {
    static volatile  int num = 0;//volatile保证可见性 但不保证原子性 理由是:store往主内存写数据时,会上锁,这时候主线(MESI)会将线程1的数据失效
    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    num++;
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    num++;
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(num);
    }
}
